var app=getApp();
var ux = require('../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    next: true,
    list: [],
    page: 0,
    obj:{},
    size: 10,
    isShow:true,  
    subUrl:"/api/delivery/saoHeOrder.do",
    action:"/api/delivery/getBHLists.do",
    acc:{},
    getAccount:"/api/delivery/getAccountsData.do",
	submit:"{{submit}}"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
	  this.setData({
      obj: wx.getStorageSync("distributor")
    })
  },
  toOut: function () {
    wx.reLaunch({
      url: '/pages/member/tab1/sugarSpice?id='+wx.getStorageSync('banrdId'),
    })
    wx.setStorageSync('distributor', "")
  },
  goHome:function(){
    wx.reLaunch({
      url: '/pages/member/tab1/sugarSpice?id='+wx.getStorageSync('banrdId'),
    })
  },
  getList: function () {
    var self = this;
    if (self.data.next == false) {
      wx.showToast({
        title: '没有更多了',
        icon: 'loading',
        duration: 1500
      });
      return;
    }
    wx.showLoading({
      mask: true,
      title: '加载中....'
    })
    var pp = {};
    ux.request(self.data.getAccount, pp, function (data) { 
        self.setData({
          obj:data.data.obj,
          acc:data.data.acc
        })
    },true);
    var params = {pageNo: parseInt(self.data.page), pageSize: self.data.size };
    ux.request(self.data.action, params, function (data) {
      var datas = data.data.list;
      if (data.data.pageInfo.next == true) {
        self.setData({
          next: true
        });
      } else {
        self.setData({
          next: false
        });
      }
      if (self.data.page == 0) {
        self.setData({
          list: datas
        });
      } else {
        self.setData({
          list: self.data.list.concat(datas)
        });
      }
      var pages = self.data.page + 1;
      self.setData({
        page: pages
      });
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
    });
  },
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo(dataset.url);
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
   if(this.data.isShow){
      this.setData({
        next: true,
        list: [],
        page: 0,
        size: 10,
        isShow:false
      })
      this.getList();
   }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    this.setData({
      isShow: true
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  toTab1: function () {
    wx.showLoading({
      title: '加载中....',
    })
    wx.redirectTo({
      url: '/pages/distributor/tab1/myOrder',
    })
  },
    saoyisao: function() {
      
      var self=this;
      wx.scanCode({
        success: (res) => {
          wx.showLoading({
            title: '处理中...',
            mask: true
          })
          var pamars = { result: res.result}
          ux.request(this.data.subUrl, pamars, function (protocol) {
           
            if (protocol.data.type==1){
              self.toast("核销成功");
              self.setData({
                isShow: true
              })
              self.onShow();
            }else{
              self.toast("核销失败");
            }
          }, true);
          setTimeout(function () { wx.hideLoading(); }, 1000);
          
        },
        fail: (res) => {
          self.toast("扫码失败");
        }
      })
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  }
})
